package com.miyake.loveviewweb.client.community;

import java.util.List;

import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.DataGrid;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.miyake.loveviewweb.client.service.DataService;
import com.miyake.loveviewweb.shared.model.GroupModel;

public class GroupListTable extends Composite implements IGroupList {

    private DataGrid<GroupModel> grid;
    private GroupListHandler handler;
    private GroupListType type;

    public GroupListTable(IGroupList.GroupListType type, GroupListHandler handler) {
        this.type = type;
        this.handler = handler;
                
        grid = new DataGrid<GroupModel>();
        initWidget(grid);
        grid.setWidth("100%");
        grid.setHeight("250px");
        
        ButtonCell buttonCell = new ButtonCell();
        Column<GroupModel, String> colButton = new Column<GroupModel, String>(buttonCell) {

            @Override
            public String getValue(GroupModel object) {
                return getButtonCaption();//object.getName();
            }

            
        };
        FieldUpdater<GroupModel, String> updater = new FieldUpdater<GroupModel, String>() {

            public void update(int index, GroupModel object, String value) {
                GroupListTable.this.handler.onSelect(object);
            }

        };
        colButton.setFieldUpdater(updater);
        grid.addColumn(colButton, getButtonCaption());

     
        grid.addColumn(new TextColumn<GroupModel>() {
            @Override
            public String getValue(GroupModel object) {
                return object.getName();
            }
        }, "Group Name");
        
        grid.addColumn(new TextColumn<GroupModel>() {
            @Override
            public String getValue(GroupModel object) {
                return object.getDescription();
            }
        }, "Description");
        
        TextColumn<GroupModel> categoryColumn = new TextColumn<GroupModel>() {
            @Override
            public String getValue(GroupModel object) {
                return "Applied Phisics";
            }
        };
        grid.addColumn(categoryColumn, "Category");
        
        
        TextColumn<GroupModel> accessColumn = new TextColumn<GroupModel>() {
            @Override
            public String getValue(GroupModel object) {
                return "Anyone";
            }
        };
        grid.addColumn(accessColumn,  "Accessibility");
        
        grid.setColumnWidth(colButton, "60px");
        grid.setColumnWidth(categoryColumn, "100px");
        grid.setColumnWidth(accessColumn, "100px");
    }

    protected String getButtonCaption() {
        return "Go";
    }

    public void setGroupListHandler(GroupListHandler handler) {
        this.handler = handler;
    }

    public void update() {
        if (type.equals(IGroupList.GroupListType.AllGroup)) {
            DataService.Util.getInstance().getAllGroups(new AsyncCallback<List<GroupModel>>() {
    
                public void onFailure(Throwable caught) {
   
                }
    
                public void onSuccess(List<GroupModel> result) {
                    grid.setRowData(result);
                }
                
            });
        }
        else if (type.equals(IGroupList.GroupListType.MyGroup)) {
            DataService.Util.getInstance().getMyGroups(new AsyncCallback<List<GroupModel>>() {

                public void onFailure(Throwable caught) {

                }

                public void onSuccess(List<GroupModel> result) {
                    grid.setRowData(result);
                }
                
            });
        }
        grid.redraw();
    }

    public Composite getComposite() {
        return this;
    }
}
